#region Copyright Jonathan Quiros

//	
// All rights are reserved. Reproduction or transmission in whole or in 
// part, in any form or by any means, electronic, mechanical or otherwise, 
// is prohibited without the prior written consent of the copyright owner.
//
// Filename: MenuHelper.cs
//

#endregion

using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using Ids.Web.DataObjects;
using Ids.Web.Security;

namespace Novagoal.Website.WebApp.Helpers
{
    public static class MenuHelper
    {
        public static MvcHtmlString CreateMenu(this UrlHelper helper, string role)
        {
            var menu = new StringBuilder();
            var securityRole = RolesProvider.GetSecurityRole(role, helper.RequestContext.HttpContext.Request);
            menu.AppendFormat("<div id='menu'><ul class='menu'>");
            foreach (var group in securityRole.MenuItems)
            {
                var first = group.Value[0];
                menu.AppendFormat("<li><a href='{0}' class='parent'><span>{1}</span></a>",
                                  helper.Action(first.Action, first.Controller), group.Key);
                menu.Append(CreateMenuItems(helper, group.Value));
                menu.AppendFormat("</li>");
            }
            menu.AppendFormat("</ul></div>");

            return new MvcHtmlString(menu.ToString());
        }

        private static string CreateMenuItems(UrlHelper url, IEnumerable<MenuItem> menuItems)
        {
            var item = new StringBuilder();
            item.AppendFormat("<div><ul>");
            foreach (var menuItem in menuItems)
            {
                item.AppendFormat("<li><a href='{0}'><span>{1}</span></a></li>", url.Action(menuItem.Action, menuItem.Controller), menuItem.Title);    
            }
            item.AppendFormat("</ul></div>");
            return item.ToString();
        }

        /// <summary>
        /// Writes the menu items according to a role into the response
        /// </summary>
        /// <param name="url">the url helper extended</param>
        /// <param name="divClass">css class for the div of the menu</param>
        /// <param name="divId">id for the div element of the menu</param>
        /// <param name="role">role in which the menu items are based</param>
        /// <param name="menuClass">css class for the menu</param>
        /// <param name="menuItemClass">css class for the menu items</param>
        public static void CreateMenu(this UrlHelper url, string divClass, string divId, string role,
                                      string menuClass, string menuItemClass)
        {
            if (string.IsNullOrEmpty(role))
            {
                return;
            }
            var securityRole = RolesProvider.GetSecurityRole(role, url.RequestContext.HttpContext.Request);

            var response = url.RequestContext.HttpContext.Response;
            var listSb = new StringBuilder();
            var itemsSb = new StringBuilder();
            var menuId = 1;

            listSb.Append("<ul>");
            foreach (var group in securityRole.MenuItems)
            {
                listSb.Append(CreateMenu(menuId, menuClass, group.Key, url, group.Value[0]));
                itemsSb.Append(CreateMenuItem(menuId, group.Value, url, menuItemClass));
                menuId++;
            }
            listSb.Append("</ul>");
            var mainTag = string.Format("<div class='{0}' id='{1}'>{2}</div>", divClass, divId, listSb);

            response.Write(mainTag);
            response.Write(itemsSb.ToString());
        }

        private static string CreateMenu(int id, string linkClass, string text, UrlHelper url, MenuItem item)
        {
            return string.Format("<li><a rel='dropmenu{0}' class='{1} {2}' href='{3}'>{4}<span class='icon ui-icon ui-icon-triangle-1-s'></span></a></li>",
                                 id, text.ToLower(), linkClass, url.Action(item.Action, item.Controller), text);
        }

        private static string CreateMenuItem(int menuId, IEnumerable<MenuItem> items, UrlHelper url, string menuItemClass)
        {
            var sb = new StringBuilder();
            foreach (var item in items)
            {
                sb.Append(string.Format("<a class='{2}' href='{0}'>{1}</a>", url.Action(item.Action, item.Controller), item.Title, menuItemClass));
            }
            return string.Format("<div class='{0}' id='dropmenu{1}'>{2}</div>", "ui-widget-content dropmenudiv", menuId, sb);
        }
    }
}